package thread.threaddemo;

/**
 * @Author: wenjingyuan
 * @Date: 2022/11/12/21:20
 * @Description:单线程下程序的执行结果
 */
public class ThreadDemo16 {
    //定义一个内部类
    static class Counter{
       private static int number=0;
       private int MAX_Counter=0;

       public void incr(){
            for (int i = 0; i < MAX_Counter; i++) {
               number++;
            }
        }
        public void decr(){
            for (int i = 0; i < MAX_Counter; i++) {
                number--;
            }

        }
        public Counter(int MAX_Counter){
           this.MAX_Counter=MAX_Counter;
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Counter counter=new Counter(100000);
//        counter.incr();
//        counter.decr();

        //我们尝试着去将这个操作放在两个线程里边去执行
        Thread t1=new Thread(()->{
            counter.incr();
        });
        Thread t2=new Thread(()->{
            counter.decr();
        });

        //启动两个线程
        t1.start();
        t2.start();
        //等待两个线程执行结束以后
        t1.join();
        t2.join();
        System.out.println("最终结果为："+counter.number);
    }
}
